#ifndef _JACK_H_
#define _JACK_H_

#include <jack/jack.h>
#include "TextException.h"

#define CONTROL_RATE  256

typedef jack_default_audio_sample_t sample_t;

class JackClient
{
      friend int onProcess(jack_nframes_t nframes, void *arg);
      friend void onShutdown(void *arg);

   public:
      static JackClient& getInstance()
      {
         static JackClient client;
         return client;
      }

      jack_nframes_t getSampleRate() { return mSampleRate; }
      void shutdown();

   private:
      JackClient() throw(TextException);
      JackClient& operator= (const JackClient& x);

      jack_client_t  *mClient;
      jack_port_t    *mInputPort;
      jack_port_t    *mOutputPort;
      jack_nframes_t  mSampleRate;

      sample_t       *mBuffer;
      size_t          mBufSize;
      pthread_mutex_t mBufMutex;
      size_t          mBufUtilized;

      void bgProcess();
      void resizeBuffer(size_t newSize);
};

#endif
